package cn.baiweigang.uu.service.fetch;

import cn.baiweigang.uu.db.dataobj.StockInfoDo;
import cn.baiweigang.uu.service.okhttp.HttpService;
import cn.baiweigang.uu.utils.StringUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class SinaService extends FetchBaseService {

    @Autowired
    private HttpService httpService;

    public void fromSina() {
        List<StockInfoDo> needSave = new ArrayList<>();
        //分页获取  每次取xx
        int pageSize = 50;
        int total = queryCNTotal();
        //计算页数
        int pages = total / pageSize;

        for (int i = 0; i < pages; i++) {
            try {
                List<StockInfoDo> pageList = queryLimt(i, pageSize);
                log.info("pageList 大小=" + pageList.size() + " 第一个=" + pageList.get(i).getDm());
                List<StockInfoDo> save = pullStockToday(pageList);
                if (save.size() > 1) {
                    needSave.addAll(save);
                }
                Thread.sleep(200);
            } catch (Throwable e) {
                log.error("从sina拉取 或处理数据异常 " + e.getMessage());
            }

        }

        //写db
        if (needSave.size() > 1) {
//            historyDayInfoRepository.insertOrUpdateBatch(needSave);
        }

    }

    private List<StockInfoDo> pullStockToday(List<StockInfoDo> pageList) {
        String url = "http://hq.sinajs.cn/list=";
        String code = "";
        for (int i = 0; i < pageList.size(); i++) {
            code += pageList.get(i).getJys() + pageList.get(i).getDm() + ",";
        }
        if (StringUtil.isNotBlank(code)) {
            code = code.substring(0, code.length() - 1);
        }
        url = url + code;

        Map<String, String> header = new HashMap<>();
        header.put("referer", "https://finance.sina.com.cn/");

        String resp = httpService.doGet(url, header, null);
        List<StockInfoDo> stockInfoDoList = new ArrayList<>();
        if (StringUtil.isNotBlank(resp)) {
            String respList[] = resp.split(";");
            for (int i = 0; i < respList.length; i++) {
                String tmp = respList[i];
                if (StringUtil.isBlank(tmp) || tmp.length() < 30) {
                    log.info("数据无内容 无需处理 " + tmp);
                    continue;
                }
                String info[] = tmp.trim().split(",");
                if (info == null || info.length < 30) {
                    log.error("从服务端拉取数据异常 " + JSONObject.toJSONString(info));
                    continue;
                }
                StockInfoDo stockInfoDo = new StockInfoDo();
                //获取DM
                stockInfoDo.setDm(info[0].substring(13, 19));
                stockInfoDo.setDs(info[30]);
                stockInfoDo.setO(Double.parseDouble(info[1]));
                stockInfoDo.setC(Double.parseDouble(info[3]));
                stockInfoDo.setH(Double.parseDouble(info[4]));
                stockInfoDo.setL(Double.parseDouble(info[5]));
                stockInfoDo.setE(parseDouble(
                        Double.parseDouble(info[9]) / 10000));
                stockInfoDo.setV(Long.parseLong(info[8]));
                stockInfoDo.setZde(parseDouble(
                        stockInfoDo.getC() - Double.parseDouble(info[2])));
                stockInfoDo.setZd(parseDouble(
                        stockInfoDo.getZde() * 100 / Double.parseDouble(info[2])));

                stockInfoDoList.add(stockInfoDo);
            }
        }

        return stockInfoDoList;
    }

    private Double parseDouble(Double value) {
        return BigDecimal.valueOf(value).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
}
